package com.lxw.oa.controller.common;

import com.lxw.oa.controller.io.BaseTo;
import com.lxw.oa.entity.SysJob;
import com.lxw.oa.entity.SysJobLog;
import com.lxw.oa.entity.User;
import com.lxw.oa.service.SysJobLogService;
import com.lxw.oa.service.SysJobService;
import com.lxw.oa.util.LogAopIn;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableTransactionManagement
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private SysJobLogService sysJobLogService;
    @Autowired
    private SysJobService jobService;

    @LogAopIn(moduleName="系统管理-定时管理",option="页面初始化")
    @GetMapping("/tojob")
    public String Log_tojob()
    {
        return "pages/schedule/Job";
    }

    @LogAopIn(moduleName="系统管理-任务日志管理",option="页面初始化")
    @GetMapping("/tojobLog")
    public String tojobLog()
    {
        return "pages/schedule/JobLog";
    }

    @PostMapping("/getInfo")
    @ResponseBody
    public BaseTo getInfo(int logId) {
       SysJobLog log=sysJobLogService.selectJobLogById(new Long((long)logId));
       return new BaseTo(log.getExceptionInfo());
    }

    @LogAopIn(moduleName="系统管理-定时管理",option="定时删除")
    @PostMapping("/remove")
    @ResponseBody
    public BaseTo remove(String ids)
    {
        try
        {
            jobService.deleteJobByIds(ids);
            return new BaseTo("");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return new BaseTo(e.getMessage());
        }
    }

    @LogAopIn(moduleName="系统管理-定时管理",option="定时暂停")
    @PostMapping("/pauseJob")
    @ResponseBody
    public BaseTo pauseJob(String ids)
    {
        try
        {
            int row=jobService.pauseJob(ids);
            return new BaseTo(row+"");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return new BaseTo("");
        }
    }


    @LogAopIn(moduleName="系统管理-定时管理",option="定时启动")
    @PostMapping("/resumeJob")
    @ResponseBody
    public BaseTo resumeJob(String ids)
    {
        try
        {
            int row=jobService.resumeJob(ids);
            return new BaseTo(row+"");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return new BaseTo("");
        }
    }

    @LogAopIn(moduleName="系统管理-定时管理",option="任务执行一次")
    @PostMapping("/jobStartNow")
    @ResponseBody
    public BaseTo jobStartNow(String id)
    {
        SysJob job=new SysJob();
        job.setJobId(Long.valueOf(id));
        try
        {
            int row=jobService.run(job);
            if(row==1){
                return new BaseTo("");
            }else{
                return new BaseTo("出错");

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return new BaseTo("出错");
        }
    }


    @LogAopIn(moduleName="系统管理-定时管理",option="定时添加")
    @PostMapping("/addJob")
    @ResponseBody
    public BaseTo addJob(SysJob sysJob)
    {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String createBy=user.getName();
        sysJob.setCreateBy(createBy);
        try{
            int row=jobService.insertJobCron(sysJob);
            if(row>0){
                return new BaseTo("");
            }else{
                return new BaseTo("未能成功添加任务信息");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new BaseTo("添加定时任务出现错误");
        }
    }

}
